package com.jxdinfo.hussar.example.scada.mqtt;

import com.alibaba.fastjson.JSON;
import com.jxdinfo.hussar.example.scada.energy.dao.MesScadaEnergyRecordMapper;
import com.jxdinfo.hussar.example.scada.energy.domain.MesScadaEnergyRecord;
import net.dreamlu.iot.mqtt.spring.client.MqttClientSubscribe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * 客户端消息监听
 *
 * @author L.cm
 */
@Service
public class MqttClientSubscribeListener {

	@Autowired
	private MesScadaEnergyRecordMapper mesScadaEnergyRecordMapper;

	@MqttClientSubscribe("push_data")
	public void subQos0(String topic, byte[] payload) {
		//将mqtt监听得到的数据转成实体类Lsjlcx
		MesScadaEnergyRecord mqttRes = JSON.parseObject(new String(payload, StandardCharsets.UTF_8), MesScadaEnergyRecord.class);
		mqttRes.setCreateTime(new Date(mqttRes.getDataTime()));
		mqttRes.setDelFlag("1");
		mesScadaEnergyRecordMapper.insertMesScadaEnergyRecord(mqttRes);
	}
}

